package com.innovation.chengzhangriji.ui.widget;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.preferences.LoginInfo;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class JoinViewNewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public interface OnClickJoinListener {
        void onClickJoin(JoinViewNewHolder holder);
    }

    private OnClickJoinListener mOnClickJoinListener;

    private CuteTextView mTextName;

    private RoundedImageView mImageHead;

    private ImageView mImageSelect,iv_sex;

    private DisplayImageOptions options; //配置图片加载及显示选项

    public JoinViewNewHolder(View itemView) {
        super(itemView);
        RelativeLayout relativeLayout = (RelativeLayout) itemView.findViewById(R.id.item_join_new_head);
        mTextName = (CuteTextView) itemView.findViewById(R.id.text_name_join_new);
        mImageHead = (RoundedImageView) itemView.findViewById(R.id.image_head_join_new);
        mImageSelect = (ImageView) itemView.findViewById(R.id.image_select_join_new);
        iv_sex = (ImageView) itemView.findViewById(R.id.image_ssex_join_new);

        relativeLayout.setOnClickListener(this);
        setOptions();
    }

    public void setOnClickJoinListener(OnClickJoinListener onClickJoinListener) {
        mOnClickJoinListener = onClickJoinListener;
    }

    public void setHead(String headUrl) {
        ImageLoader.getInstance().displayImage(headUrl, mImageHead, options);
    }

    public void setSex(int sexfor) {
        switch (sexfor){
            case 1:
                iv_sex.setVisibility(View.VISIBLE);
                iv_sex.setImageResource(R.mipmap.icon_nan);
                break;
            case 2:
                iv_sex.setVisibility(View.VISIBLE);
                iv_sex.setImageResource(R.mipmap.icon_nv);
                break;
            default:
                iv_sex.setVisibility(View.INVISIBLE);
                break;
        }
    }

    public void setText(CharSequence text) {
        mTextName.setText(text);
    }

    public void setSelectVisibility(boolean visibility) {
        if (visibility) {
            mImageSelect.setVisibility(View.VISIBLE);
        } else {
            mImageSelect.setVisibility(View.INVISIBLE);
        }
    }

    private void setOptions() {
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.gray_head)    //在ImageView加载过程中显示图片
                .showImageForEmptyUri(R.mipmap.gray_head)  //image连接地址为空时
                .showImageOnFail(R.mipmap.gray_head)  //image加载失败
                        // .cacheInMemory(true)  //加载图片时会在内存中加载缓存
                .cacheOnDisk(true)   //加载图片时会在磁盘中加载缓存
                .build();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.item_join_new_head) {
            if (mOnClickJoinListener != null) {
                mOnClickJoinListener.onClickJoin(this);
            }
        }
    }
}
